package com.wl.thread.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 实现多线程同步下载图片
 */
public class TestThread04 implements Runnable{
    //网络图片地址
    private String url;
    //保存文件名称
    private String name;

    public TestThread04(String url,String name){
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownloaderRunnable webDownloaderRunnable = new WebDownloaderRunnable();
        webDownloaderRunnable.downloader(url,name);
        System.out.println("下载文件名为:"+name);
    }

    public static void main(String[] args) {
        TestThread02 t1 = new TestThread02("https://gitee.com/carrywl/image-hosting/raw/master/image-codeStudy/avatar.jpeg","1.jpg");
        TestThread02 t2 = new TestThread02("https://gitee.com/carrywl/image-hosting/raw/master/image-codeStudy/weChat-Code.jpg","2.jpg");
        TestThread02 t3 = new TestThread02("https://gitee.com/carrywl/image-hosting/raw/master/image-codeStudy/QQ图片20200809153256.jpg","3.jpg");

        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();
    }
}

/**
 * 下载器
 */
class WebDownloaderRunnable{
    //下载方法
    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,downloader方法出现问题");
        }
    }
}
